package com.probiz.estoremf.sales.web.action;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.sales.PromoRule;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.sales.service.PromoRuleManager;

public class PromoRuleFrontController extends GenericStoreFrontController<PromoRule> {
	private PromoRuleManager promoRuleManager;
	public void setPromoRuleManager(PromoRuleManager promoRuleManager) {
		this.promoRuleManager = promoRuleManager;
	}
	protected void initController() throws Exception {
		mgr = promoRuleManager; 
		
	}
	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		
		ModelAndView mv = new ModelAndView("sales/defaultPromoRuleDetail");
		Integer promoRuleId = promoRuleManager.getPormoRuleIdByUri(request.getRequestURI());
		PromoRule promoRule = promoRuleManager.getById(promoRuleId);
		if(null == promoRule){
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}	
		promoRuleManager.setState(promoRule);
		//只有激活和将来的才能显示
		if(!String.valueOf(PromoRule.STATE_DOING).equals(promoRule.getState()) && !String.valueOf(PromoRule.STATE_FUTURE).equals(promoRule.getState())){
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		mv.addObject("promoRule", promoRule);
		
		return mv;
	}

}
